import { acceptHMRUpdate, defineStore } from 'pinia'
// import type { EnumLanguageKeys } from '~/modules/i18n'
// import type { EnumCurrencyKey } from '~/apis/types'
// import { TIMEZONEABBR } from '~/utils/enums'

/** 语言对应配置 */
export const languageConfig: Record<EnumLanguageKeys, {
  /** 默认货币 */
  currency: EnumCurrencyKey
}> = {
  'zh-CN': {
    currency: 'CNY',
  },
  'pt-BR': {
    currency: 'BRL',
  },
  'vi-VN': {
    currency: 'KVND',
  },
  'th-TH': {
    currency: 'THB',
  },
  'hi-IN': {
    currency: 'INR',
  },
  'en-US': {
    currency: 'USDT',
  },
  'in-ID': {
    currency: 'IDR',
  },
  'ko-KR': {
    currency: 'KRW',
  },
  'ja-JP': {
    currency: 'JPY',
  },
  'aa-AA': {
    currency: 'CNY',
  },
}

export const useLanguageStore = defineStore('language', () => {
  const router = useRouter()
  // const { closeLeftSidebar } = useLeftSidebar()

  /** 所有语言列表 */
  const AllLanguages = ref<Array<{
    title: string
    path: string
    icon: string
    value: EnumLanguageKeys
    phone: string
    phoneId: string
    prefix: string
    offset: number
    zone: TIMEZONEABBR
  }>>([
    {
      title: '简体中文',
      path: '',
      icon: '',
      value: 'zh-CN',
      phone: '+86',
      phoneId: '1137',
      prefix: 'zh',
      offset: 8,
      zone: TIMEZONEABBR.CST,
    },
    {
      title: 'English',
      path: '',
      icon: '',
      value: 'en-US',
      phone: '+1',
      phoneId: '1283',
      prefix: 'en',
      offset: -4,
      zone: TIMEZONEABBR.USA,
    },
    {
      title: 'Tiếng Việt',
      path: '',
      icon: '',
      value: 'vi-VN',
      phone: '+84',
      phoneId: '1287',
      prefix: 'vn',
      offset: 7,
      zone: TIMEZONEABBR.VIETNAM,
    },
    {
      title: 'Português',
      path: '',
      icon: '',
      value: 'pt-BR',
      phone: '+55',
      phoneId: '1125',
      prefix: 'pt',
      offset: -3,
      zone: TIMEZONEABBR.BRT,
    },
    {
      title: 'ภาษาไทย',
      path: '',
      icon: '',
      value: 'th-TH',
      phone: '+66',
      phoneId: '66',
      prefix: 'th',
      offset: 7,
      zone: TIMEZONEABBR.THAILAND,
    },
    {
      title: 'हिन्दी',
      path: '',
      icon: '',
      value: 'hi-IN',
      phone: '+91',
      phoneId: '91',
      prefix: 'hi',
      offset: 5.5,
      zone: TIMEZONEABBR.INDIA,
    },
    {
      title: 'Indonesia',
      path: '',
      icon: '',
      value: 'in-ID',
      phone: '+62',
      phoneId: '62',
      prefix: 'id',
      offset: 7,
      zone: TIMEZONEABBR.INDONESIA,
    },
    {
      title: '한국어',
      path: '',
      icon: '',
      value: 'ko-KR',
      phone: '+82',
      phoneId: '82',
      prefix: 'ko',
      offset: 9,
      zone: TIMEZONEABBR.KOREA,
    },
    {
      title: '日本語',
      path: '',
      icon: '',
      value: 'ja-JP',
      phone: '+81',
      phoneId: '81',
      prefix: 'ja',
      offset: 9,
      zone: TIMEZONEABBR.JAPAN,
    },
  ])

  /** 当前选择的语言 */
  const userLanguage = ref(getCurrentLanguageForFrontend())

  const langTimeOffset = computed(() => AllLanguages.value.filter(a => a.value === userLanguage.value)[0].offset)
  const currentLangZone = computed(() => AllLanguages.value.filter(a => a.value === userLanguage.value)[0].zone)
  /** 当前是中文 */
  const isZhcn = computed(() => userLanguage.value === 'zh-CN')

  /** 当前是葡萄牙语 */
  const isPtbr = computed(() => userLanguage.value === 'pt-BR')

  /** 更换语言 */
  async function changeLanguage(enumLangKeys: EnumLanguageKeys) {
    Local.remove(STORAGE_CURRENT_GLOBAL_CURRENCY_KEY)
    Local.remove(STORAGE_MENU_EXPAND_DOMID)
    Local.set(STORAGE_CURRENT_LANGUAGE, enumLangKeys)
    const currentUrl = getCurrentUrlLanguage()
    window.langChoosed = enumLangKeys as any
    const _fullPath = router.currentRoute.value.fullPath.replace(`/${currentUrl}`, getLocalUrlToUrlLang(enumLangKeys))
    // closeLeftSidebar()
    setTimeout(() => {
      location.href = `${location.origin}/${_fullPath}`
    }, 500)
  }

  return {
    AllLanguages,
    userLanguage,
    langTimeOffset,
    currentLangZone,
    isZhcn,
    isPtbr,
    changeLanguage,
  }
})

if (import.meta.hot)
  import.meta.hot.accept(acceptHMRUpdate(useLanguageStore, import.meta.hot))
